package com.topscomm.mpm.script;

import cn.hutool.core.collection.CollectionUtil;
import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.main.hessian.IMmsDeptExtraInfoService;
import com.topscomm.mpm.MpmScript;
import com.topscomm.mpm.MpmSystemConst;
import com.topscomm.mpm.pojo.MpmProjectAppointmentEntity;
import com.topscomm.mpm.pojo.MpmProjectEntity;
import com.topscomm.mpm.pojo.MpmProjectPlanEntity;
import com.topscomm.mpm.pojo.MpmProjectTeamEntity;
import com.topscomm.mpm.service.impl.*;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MpmAppointmentSendEmailScript extends MpmScript {

    /**
     * 签发人审批后需要生成任命书编号
     *
     * @param service
     * @param entity
     * @throws Exception
     */
    @Transactional
    public void sendEmail(ITapService service, TapEntity entity) throws Exception {
        // 审批节点实例
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
        MpmProjectAppointmentService mpmProjectAppointmentService = (MpmProjectAppointmentService) SpringUtil.getBean("mpmProjectAppointmentService");

        // 获取任命书信息
        MpmProjectAppointmentEntity mpmProjectAppointmentEntity = mpmProjectAppointmentService.queryById(sourceid);
        if (mpmProjectAppointmentService.judgeSendTime(null, mpmProjectAppointmentEntity.getCreateon())) {
            return;
        }

        // 重新生成任命书
        mpmProjectAppointmentService.sendMessage(ConvertUtil.convertToString(mpmProjectAppointmentEntity.getId()));
    }

    /**
     * @author
     * @date
     * @description: 新增任命结束，给项目组成员发送消息
     * @modify:
     */
    @Transactional
    public void projectAppointmentSendMessage(ITapService service, TapEntity entity) throws Exception {
        // 审批点实例
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
        String sourcetype = ConvertUtil.convertToString(nodeins.getSourcetype());
        MpmProjectTeamService mpmProjectTeamService = (MpmProjectTeamService) SpringUtil.getBean("mpmProjectTeamService");
        MpmProjectAppointmentService mpmProjectAppointmentService = (MpmProjectAppointmentService) SpringUtil.getBean("mpmProjectAppointmentService");
        MpmProjectService mpmProjectService = (MpmProjectService) SpringUtil.getBean("mpmProjectService");
        MpmProjectBidScoreService mpmProjectBidScoreService = (MpmProjectBidScoreService) SpringUtil.getBean("mpmProjectBidScoreService");
        // 获取任命书信息
        MpmProjectAppointmentEntity mpmProjectAppointmentEntity = mpmProjectAppointmentService.queryById(sourceid);
        // 查询项目编号
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle("projectcode = '{0}'", mpmProjectAppointmentEntity.getProjectcode());
        // 获取项目信息
        MpmProjectEntity mpmProjectEntity = mpmProjectService.queryObjectByWhere(sql.toString());
        // 查询项目是否为第一次任命
        int count = mpmProjectAppointmentService.getCount(sql.toString());
        if (count > 1) {
            // 消息内容
            String contentInfo = "任命";
            // 如果项目经理非省总，给项目经理发送消息
            mpmProjectTeamService.sendMessageToPM(sourceid, sourcetype, contentInfo, mpmProjectEntity);
            // 任命变更给销售PMO和省区PMO发送消息提醒
            mpmProjectTeamService.sendMessageAppointmentToPMO(mpmProjectAppointmentEntity, mpmProjectEntity);
        } else if (count == 1) {
            // 省招A级项目，任命审批通过后对项目经理发送第一次自评提醒
            mpmProjectBidScoreService.sendBidScoreMessageAfterAppointment(mpmProjectEntity);
        }
    }

    /**
     * @author:zhangjunyao
     * @date:2021-11-12
     * @description: C、D级项目任命审批通过后，给项目组成员发送App消息
     * @modify:
     */
    @Transactional
    public void SendAppMessageToCDProjectGroupMember(ITapService service, TapEntity entity) throws Exception {
        // 审批点实例
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
        String sourcetype = ConvertUtil.convertToString(nodeins.getSourcetype());
        MpmProjectService mpmProjectService = (MpmProjectService) SpringUtil.getBean("mpmProjectService");
        MpmProjectTeamService mpmProjectTeamService = (MpmProjectTeamService) SpringUtil.getBean("mpmProjectTeamService");
        MpmProjectAppointmentService mpmProjectAppointmentService = (MpmProjectAppointmentService) SpringUtil.getBean("mpmProjectAppointmentService");
        // 获取任命书信息
        MpmProjectAppointmentEntity mpmProjectAppointmentEntity = mpmProjectAppointmentService.queryById(sourceid);
        // 首次任命发送此消息,任命变更不发送
        if (!"1".equals(mpmProjectAppointmentEntity.getSyspush())) {
            return;
        }
        // 查询项目编号
        String projectCode = mpmProjectAppointmentEntity.getProjectcode();
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle("projectcode = '{0}'", projectCode);
        // 给项目组成员发送App消息
        List<MpmProjectEntity> mpmProjectEntities = mpmProjectService.queryByWhere(sql.toString());
        if (CollectionUtils.isEmpty(mpmProjectEntities)) {
            return;
        }
        MpmProjectEntity mpmProjectEntity = mpmProjectEntities.get(0);
//        List<String> levelList = new ArrayList<String>();
//        levelList.add(MpmSystemConst.ProjectLevel.C);
//        levelList.add(MpmSystemConst.ProjectLevel.D);
//        if (levelList.contains(mpmProjectEntity.getProjectlevel())) {
            mpmProjectTeamService.sendAppMessageToCDProjectGroupMember(mpmProjectEntity, sourceid, sourcetype);
//        }
    }
}
